package cn.anline.ann_wp.utils

import android.content.Context
import android.graphics.Bitmap
import android.media.MediaMetadataRetriever
import android.provider.MediaStore
import cn.anline.ann_wp.bean.VideoInfo
import java.util.ArrayList

/**
 * Created by luozh on 2017/7/12.
 */

object VideoUtils {

    /**
     * 数据库操作，如果内容多了会阻塞线程， 尽量在线程中获取
     * @param context
     * *
     * @return
     */
    fun getVideoInfoList(context: Context?): List<VideoInfo>? {
        var list: MutableList<VideoInfo>? = null
        if (context == null) return null

        val cursor = context.contentResolver.query(
                MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null, null) ?: return null

        list = ArrayList<VideoInfo>()
        while (cursor.moveToNext()) {
            val id = cursor.getInt(cursor
                    .getColumnIndexOrThrow(MediaStore.Video.Media._ID))
            val title = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE))
            val album = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.ALBUM))
            val artist = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST))
            val displayName = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME))
            val mimeType = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE))
            val path = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.DATA))
            val duration = cursor
                    .getInt(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)).toLong()
            val size = cursor
                    .getLong(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE))
            val video = VideoInfo(id, title, album, artist, displayName,
                    mimeType, path, size, duration)
            list.add(video)
        }
        cursor.close()


        return list
    }


    /**
     * 给出url，获取视频的第一帧

     * @param fileName
     * *
     * @return
     */
    fun getVideoThumbnail(fileName: String): Bitmap? {
        var bitmap: Bitmap? = null
        //MediaMetadataRetriever 是android中定义好的一个类，提供了统一
        //的接口，用于从输入的媒体文件中取得帧和元数据；
        val retriever = MediaMetadataRetriever()
        try {
            //根据文件路径获取缩略图
            retriever.setDataSource(fileName)
            // retriever.setDataSource(url , new HashMap());这是现上的url
            //获得第一帧图片
            bitmap = retriever.frameAtTime
        } catch (e: IllegalArgumentException) {
            e.printStackTrace()
        } finally {
            retriever.release()
        }
        return bitmap
    }

}